package com.shaozi.nc.data.mongodao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.shaozi.data.mq.business.bean.MqTargetUserBean;
import com.shaozi.lib.throwable.SzException;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.mq.bean.base.CommonExtraBean;

@Repository("secretaryMDao")
public class SecretaryMDao extends BaseSecretaryMDao<SecretaryModel> {

	@Autowired
	public SecretaryMDao(MongoTemplate shaoziTemplate) {
		super(shaoziTemplate);
	}

	public SecretaryModel findByExtraId(String companyId, String userId, Integer extraId, int sourceType)
			throws SzException {
		Query query = new Query();
		Criteria c = new Criteria();
		c.and(SecretaryModel.FIELDNAME_extraBean + "." + CommonExtraBean.FIELDNAME_id).is(extraId);
		c.and(SecretaryModel.FIELDNAME_sourceType).is(sourceType);

		Criteria cc = new Criteria(MqTargetUserBean.FIELDNAME_userId).is(userId);
		c.and(SecretaryModel.FIELDNAME_users).elemMatch(cc);
		query.addCriteria(c);

		return this.getMongoTemplate().findOne(query, SecretaryModel.class, getCollectionName(companyId));

	}

	public static void main(String[] args) {
		int sourceType = 8001;
		int extraId = 240703;
		String userId = "10158";
		Query query = new Query();
		Criteria c = new Criteria();
		c.and(SecretaryModel.FIELDNAME_extraBean + "." + CommonExtraBean.FIELDNAME_id).is(extraId);
		c.and(SecretaryModel.FIELDNAME_sourceType).is(sourceType);

		Criteria cc = new Criteria(MqTargetUserBean.FIELDNAME_userId).is(userId);
		c.and(SecretaryModel.FIELDNAME_users).elemMatch(cc);
		query.addCriteria(c);

		System.out.println(query.toString());
	}

}
